package com.xxyf.eoro;

import com.xxyf.tools.Rest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionControllerHandler {

    /**
     * 拦截所有程序异常
     * @param request
     * @param ex
     * @return
     */
    @ExceptionHandler(value= AccessDeniedException.class)
    public Rest error(HttpServletRequest request, Exception ex ){
        return new Rest(1002,null,"权限不足");
    }
    @ExceptionHandler(value = AuthenticationException.class)
    public Rest erro(HttpServletRequest request, Exception ex){
        return new Rest(1003,null,"登录过期");
    }

    @ExceptionHandler(value=RuntimeException.class)
    public Rest errorHandler(HttpServletRequest request, Exception ex ){
        log.error(ex.getMessage());
        return Rest.error("服务器错误");
    }

    /**
     * SQLException异常
     * @param request
     * @param ex
     * @return
     */
    @ExceptionHandler(value= SQLException.class)
    public Rest errorHandlerSQL(HttpServletRequest request,Exception ex){
        log.error(ex.getMessage());
        return Rest.error("服务器错误");
    }
}
